package com.kxq.zhuoyue.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kxq.zhuoyue.po.RedEnvelopeTemplate;
import com.kxq.zhuoyue.service.RedEnvelopeTemplateService;
import com.kxq.zhuoyue.util.result.AjaxResult;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lyz
 * @since 2023年09月01日
 */
@RestController
@RequestMapping("/redEnvelopeTemplate")
public class RedEnvelopeTemplateController {

    @Autowired
    private RedEnvelopeTemplateService redEnvelopeTemplateService ;


    @PostMapping("/findByPage")
    public AjaxResult findByPage(@RequestBody RedEnvelopeTemplate redEnvelopeTemplate){
        Page<RedEnvelopeTemplate> page = new Page<>(redEnvelopeTemplate.getPageNum(),redEnvelopeTemplate.getPageSize());
        IPage<RedEnvelopeTemplate> ipage = redEnvelopeTemplateService.page(page);
        List<RedEnvelopeTemplate> list = ipage.getRecords();
        return AjaxResult.success("查询成功",list);
    }

    @PostMapping("/addRed")
    public AjaxResult add(@RequestBody RedEnvelopeTemplate redEnvelopeTemplate){
        boolean add = redEnvelopeTemplateService.save(redEnvelopeTemplate);
        if(add){
            return AjaxResult.success("添加成功",add);
        }else{
            return AjaxResult.error("添加失败",add);
        }
    }

    @PutMapping("/updateRed")
    public AjaxResult marge(@RequestBody RedEnvelopeTemplate redEnvelopeTemplate){
        boolean upd = redEnvelopeTemplateService.updateById(redEnvelopeTemplate);
        if(upd){
            return AjaxResult.success("修改成功",upd);
        }else{
            return AjaxResult.error("修改失败",upd);
        }
    }

    @DeleteMapping("/delRed/{redId}")
    public AjaxResult delete(@PathVariable String redId){
        boolean del = redEnvelopeTemplateService.removeById(redId);
        if(del){
            return AjaxResult.success("删除成功",del);
        }else{
            return AjaxResult.error("删除失败",del);
        }
    }


}


